<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/3/27
 * Time: 10:31
 */

namespace app\index\controller;
use app\admin\model\AssembleList;
use app\admin\model\AssembleUser;
use app\admin\model\GoodsList;
use app\admin\model\OrderList;
use app\admin\model\WechatConfig;
use think\Controller;

class Notify extends Controller
{
    //商城订单支付成功
    public function mackupNotify(){
        $raw_xml = file_get_contents("php://input");
        libxml_disable_entity_loader(true);
        $ret = json_decode(json_encode(simplexml_load_string($raw_xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        if ($ret['return_code'] === "SUCCESS"&&$ret['result_code'] === "SUCCESS"){
            $orderid=$ret['out_trade_no'];
            $find=OrderList::where('order_no',$orderid)->find();
            if($find['order_status']==0){
                $data=[
                    'pay_no'=>$ret['transaction_id'],
                    'order_status'=>1,
                    'pay_time'=>time()
                ];
                OrderList::where('order_no',$orderid)->update($data);
                if ($find['test_type'] == 1){
                    $sell = GoodsList::where('id',$find['good_id'])->value('subscription');
                    GoodsList::update(['subscription'=>$sell+1],['id'=>$find['good_id']]);
                }
                if ($find['order_type'] == 2){
                    $ass = AssembleList::where('good_id',$find['good_id'])->find();
                    $hours = $ass['hours'] * 3600;
                    if ($find['f_id'] == 0){
                        $data = [
                            'user_id'=>$find['user_id'],
                            'good_id'=>$find['good_id'],
                            'order_id'=>$find['id'],
                            'number'=>$ass['number']-1,
                            'end_time'=>time() + $hours,
                        ];
                        AssembleUser::create($data);
                    }else{
                        if ($ass['number']-1 > 0){
                            AssembleUser::update(['number'=>$ass['number']-1],['id'=>$ass['id']]);
                        }else{
                            AssembleUser::update(['number'=>$ass['number']-1,'status'=>2],['id'=>$ass['id']]);
                        }
                    }
                }
                $wechatRes=WechatConfig::where('wx_id',1)->find();
                $send=new \app\index\controller\Send();
                $mobile=$find['phone'];
                $messageType='SMS_166376811';
                $code=['title'=>$find['good_name'],'order_no'=>$find['order_no'],'phone'=>$wechatRes['phone']];
                $send->sms($mobile,$code,$messageType);

                echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
            }
        }
    }


}